package com.liuwei.netty.start;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ByteBufDemo {
    public static void main(String[] args) {
        // 创建一个容量为10的byteBuf
        ByteBuf buf = Unpooled.buffer(10);
        log.info("buf={}", buf);

        // 添加数据
        for (int i = 0; i < 5; i++) {
            buf.writeByte(i);
        }
        log.info("after buf={}", buf);

        // 按照索引获取数据
        for (int i = 0; i < 3; i++) {
            log.info(String.valueOf(buf.getByte(i)));
        }
        log.info("after buf={}", buf);

        // 按照索引读取数据
        for (int i = 0; i < 3; i++) {
            log.info(String.valueOf(buf.readByte()));
        }
        log.info("after buf={}", buf);
        // 获取读指针
        log.info("读指针" + buf.readerIndex());
        // 获取写指针
        log.info("写指针" + buf.writerIndex());
        // 重新设置写指针
        log.info("写指针" + buf.writerIndex(6));
        // 获取容量
        log.info("容量" + buf.capacity());

    }
}
